Component({
  options:{
    multipleSlots: true,
    styleIsolation: 'isolated'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: Array,
      observer(newVal){
        this.setData({
          currentValue: newVal
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    currentValue: [0,0],
    preValue: [0,0],
    regionTree: {
      label: '新疆维吾尔自治区',
      value: '65',
      children: [
        {
          label: '乌鲁木齐市',
          value: '6501',
          children: [
            { label: '天山区', value: '650102', children: [] },
            { label: '沙依巴克区', value: '650103', children: [] },
            { label: '新市区', value: '650104', children: [] },
            { label: '水磨沟区', value: '650105', children: [] },
            { label: '头屯河区', value: '650106', children: [] },
            { label: '达坂城区', value: '650107', children: [] },
            { label: '米东区', value: '650109', children: [] },
            { label: '乌鲁木齐县', value: '650121', children: [] }
          ]
        },
        {
          label: '克拉玛依市',
          value: '6502',
          children: [
            { label: '独山子区', value: '650202', children: [] },
            { label: '克拉玛依区', value: '650203', children: [] },
            { label: '白碱滩区', value: '650204', children: [] },
            { label: '乌尔禾区', value: '650205', children: [] }
          ]
        },
        {
          label: '吐鲁番市',
          value: '6504',
          children: [
            { label: '高昌区', value: '650402', children: [] },
            { label: '鄯善县', value: '650421', children: [] },
            { label: '托克逊县', value: '650422', children: [] }
          ]
        },
        {
          label: '哈密市',
          value: '6505',
          children: [
            { label: '伊州区', value: '650502', children: [] },
            { label: '巴里坤哈萨克自治县', value: '650521', children: [] },
            { label: '伊吾县', value: '650522', children: [] }
          ]
        },
        {
          label: '昌吉回族自治州',
          value: '6523',
          children: [
            { label: '昌吉市', value: '652301', children: [] },
            { label: '阜康市', value: '652302', children: [] },
            { label: '呼图壁县', value: '652323', children: [] },
            { label: '玛纳斯县', value: '652324', children: [] },
            { label: '奇台县', value: '652325', children: [] },
            { label: '吉木萨尔县', value: '652327', children: [] },
            { label: '木垒哈萨克自治县', value: '652328', children: [] }
          ]
        },
        {
          label: '博尔塔拉蒙古自治州',
          value: '6527',
          children: [
            { label: '博乐市', value: '652701', children: [] },
            { label: '阿拉山口市', value: '652702', children: [] },
            { label: '精河县', value: '652722', children: [] },
            { label: '温泉县', value: '652723', children: [] }
          ]
        },
        {
          label: '巴音郭楞蒙古自治州',
          value: '6528',
          children: [
            { label: '库尔勒市', value: '652801', children: [] },
            { label: '轮台县', value: '652822', children: [] },
            { label: '尉犁县', value: '652823', children: [] },
            { label: '若羌县', value: '652824', children: [] },
            { label: '且末县', value: '652825', children: [] },
            { label: '焉耆回族自治县', value: '652826', children: [] },
            { label: '和静县', value: '652827', children: [] },
            { label: '和硕县', value: '652828', children: [] },
            { label: '博湖县', value: '652829', children: [] }
          ]
        },
        {
          label: '阿克苏地区',
          value: '6529',
          children: [
            { label: '阿克苏市', value: '652901', children: [] },
            { label: '库车市', value: '652902', children: [] },
            { label: '温宿县', value: '652922', children: [] },
            { label: '沙雅县', value: '652924', children: [] },
            { label: '新和县', value: '652925', children: [] },
            { label: '拜城县', value: '652926', children: [] },
            { label: '乌什县', value: '652927', children: [] },
            { label: '阿瓦提县', value: '652928', children: [] },
            { label: '柯坪县', value: '652929', children: [] }
          ]
        },
        {
          label: '克孜勒苏柯尔克孜自治州',
          value: '6530',
          children: [
            { label: '阿图什市', value: '653001', children: [] },
            { label: '阿克陶县', value: '653022', children: [] },
            { label: '阿合奇县', value: '653023', children: [] },
            { label: '乌恰县', value: '653024', children: [] }
          ]
        },
        {
          label: '喀什地区',
          value: '6531',
          children: [
            { label: '喀什市', value: '653101', children: [] },
            { label: '疏附县', value: '653121', children: [] },
            { label: '疏勒县', value: '653122', children: [] },
            { label: '英吉沙县', value: '653123', children: [] },
            { label: '泽普县', value: '653124', children: [] },
            { label: '莎车县', value: '653125', children: [] },
            { label: '叶城县', value: '653126', children: [] },
            { label: '麦盖提县', value: '653127', children: [] },
            { label: '岳普湖县', value: '653128', children: [] },
            { label: '伽师县', value: '653129', children: [] },
            { label: '巴楚县', value: '653130', children: [] },
            { label: '塔什库尔干塔吉克自治县', value: '653131', children: [] }
          ]
        },
        {
          label: '和田地区',
          value: '6532',
          children: [
            { label: '和田市', value: '653201', children: [] },
            { label: '和田县', value: '653221', children: [] },
            { label: '墨玉县', value: '653222', children: [] },
            { label: '皮山县', value: '653223', children: [] },
            { label: '洛浦县', value: '653224', children: [] },
            { label: '策勒县', value: '653225', children: [] },
            { label: '于田县', value: '653226', children: [] },
            { label: '民丰县', value: '653227', children: [] }
          ]
        },
        {
          label: '伊犁哈萨克自治州',
          value: '6540',
          children: [
            { label: '伊宁市', value: '654002', children: [] },
            { label: '奎屯市', value: '654003', children: [] },
            { label: '霍尔果斯市', value: '654004', children: [] },
            { label: '伊宁县', value: '654021', children: [] },
            { label: '察布查尔锡伯自治县', value: '654022', children: [] },
            { label: '霍城县', value: '654023', children: [] },
            { label: '巩留县', value: '654024', children: [] },
            { label: '新源县', value: '654025', children: [] },
            { label: '昭苏县', value: '654026', children: [] },
            { label: '特克斯县', value: '654027', children: [] },
            { label: '尼勒克县', value: '654028', children: [] }
          ]
        },
        {
          label: '塔城地区',
          value: '6542',
          children: [
            { label: '塔城市', value: '654201', children: [] },
            { label: '乌苏市', value: '654202', children: [] },
            { label: '沙湾市', value: '654203', children: [] },
            { label: '额敏县', value: '654221', children: [] },
            { label: '托里县', value: '654224', children: [] },
            { label: '裕民县', value: '654225', children: [] },
            { label: '和布克赛尔蒙古自治县', value: '654226', children: [] }
          ]
        },
        {
          label: '阿勒泰地区',
          value: '6543',
          children: [
            { label: '阿勒泰市', value: '654301', children: [] },
            { label: '布尔津县', value: '654321', children: [] },
            { label: '富蕴县', value: '654322', children: [] },
            { label: '福海县', value: '654323', children: [] },
            { label: '哈巴河县', value: '654324', children: [] },
            { label: '青河县', value: '654325', children: [] },
            { label: '吉木乃县', value: '654326', children: [] }
          ]
        },
        {
          label: '自治区直辖县级行政区划',
          value: '6590',
          children: [
            { label: '石河子市', value: '659001', children: [] },
            { label: '阿拉尔市', value: '659002', children: [] },
            { label: '图木舒克市', value: '659003', children: [] },
            { label: '五家渠市', value: '659004', children: [] },
            { label: '北屯市', value: '659005', children: [] },
            { label: '铁门关市', value: '659006', children: [] },
            { label: '双河市', value: '659007', children: [] },
            { label: '可克达拉市', value: '659008', children: [] },
            { label: '昆玉市', value: '659009', children: [] },
            { label: '胡杨河市', value: '659010', children: [] },
            { label: '新星市', value: '659011', children: [] },
            { label: '白杨市', value: '659012', children: [] },
          ]
        }
      ]
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleOpenDialog(){
      const that = this
      const preValue = this.data.preValue
      const obj = {
        show: true,
        currentValue: preValue
      }
      if(that.properties.value?.length) {
        obj.currentValue = that.properties.value
      }
      this.setData({
        ...obj
      })
    },
    handleCloseDialog(){
      this.setData({
        show:false
      })
    },
    handleCancel(){
      this.setData({
        show:false
      })
    },
    handleConfirm(){
      const value = this.data.currentValue
      const level1 = this.data.regionTree.children[value[0]]
      const level2 = level1.children[value[1]]
      this.triggerEvent('change',{
        value,
        info: [{
          label: level1.label,
          value: level1.value
        },{
          label: level2.label,
          value: level2.value
        }]
      })
      this.setData({
        show: false,
        currentValue: value,
        preValue: value
      })
    },
    bindChange(e){
      const val = e.detail.value;
      const level1 = this.data.regionTree.children[val[0]]
      const level2 = level1.children
      this.setData({
        currentValue: [val[0],val[1]>level2.length-1?level2.length-1:val[1]]
      })
    }
  }
})